home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 29 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. Path: in1.uu.net!bounce-back
  2. Date: 10 Jan 96 23:18:50 GMT
  3. Approved: fjh@cs.mu.oz.au
  4. From: clamage@Eng.Sun.COM (Steve Clamage)
  5. Newsgroups: comp.std.c++
  6. Subject: Re: ambiguous or not ?
  7. X-Original-Date: 10 Jan 1996 16:38:22 GMT
  8. Organization: Sun Microsystems Inc.
  9. Message-ID: <4d0q1u$il8@engnews1.Eng.Sun.COM>
  10. References: <199601092222.WAA27727@eiger.pncl.co.uk>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMPRJleEDnX0m9pzZAQG66AF+J8Kczs/xd49BpNhpBE/ePY6GK8RfEooT
  14.     icLlc7gtow4agspZC4W0PYtUJP3/jVW0
  15.     =VRAC
  16.  
  17. In article WAA27727@eiger.pncl.co.uk, dcb@pncl.co.uk (David C Binderman)
  18. writes:
  19.  
  20. Ignoring the several errors in this sample code, the comparison is not
  21. ambiguous. You are comparing, in effect, the types
  22.     R == enum
  23.  
  24. There are two viable functions:
  25. 1. built-in operator==(int,int)
  26. 2. operator==(char, const S&)
  27. (None of the other built-in comparison "functions" could be called.)
  28.  
  29. Function 1 requires a user-defined conversion on the first argument and
  30. a promotion on the second.
  31.  
  32. Function 2 requires a user-defined conversion on both arguments (plus
  33. extra standard conversions which don't affect the analysis).
  34.  
  35. Thus, Function 1 is preferred, and there is no ambiguity.
  36.  
  37. There would be an ambiguity in choosing Function 2, since the enum could be
  38. converted to an S by first converting it to a char or unsigned char. But
  39. since Function 1 is unconditionally preferred, it doesn't matter how many
  40. potential ambiguities exist in using functions which are not preferred.
  41.  
  42. ---
  43. Steve Clamage, stephen.clamage@eng.sun.com
  44.  
  45.  
  46. >Following on from an outbreak of compiler disagreement,
  47.  
  48. >// is this ambiguous or not ?
  49.  
  50. >// workaround is to use cast on lhs
  51.  
  52. >#include <iostream.h>
  53.  
  54. >class S
  55. >{
  56.  
  57. >public:
  58. >    S( char ) {
  59. >        cerr << "S::S char\n";
  60. >    };
  61. >    
  62. >    S( unsigned char ) {
  63. >        cerr << "S::S char\n"
  64. >    };
  65. >};
  66.  
  67. >operator == ( char, const S &)
  68. >{
  69. >    cerr << "operator ==\n";
  70. >};
  71.  
  72. >class R
  73. >{
  74. >public:
  75. >    enum { a, b, c};
  76. >    operator int () {
  77. >        cerr << "R::operator int\n";
  78. >    };
  79. >};
  80.  
  81. >int
  82. >main()
  83. >{
  84. >    R z;
  85. >    // if ((int) z == R::a)
  86. >    // workaround line
  87. >    if (z == R::a)
  88. > // ambiguous ?
  89. >        ;
  90. >}
  91. ---
  92. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  93.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  94.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  95.